//
//  JYRechargeCell.m
//  JYFarm
//
//  Created by 花花 on 2017/10/11.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYRechargeCell.h"

@implementation JYRechargeCell


- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];
    
    self.selImgView.hidden = !selected;
   
}
+ (instancetype)cellWithTableView:(UITableView *)tableView{

     static NSString *ID = @"JYRechargeCell";
    
    JYRechargeCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[JYRechargeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubView];
    }
    return self;

}

- (void)setupSubView{

    [self.contentView addSubview:self.imgView];
    [self.contentView addSubview:self.selImgView];
    [self.contentView addSubview:self.titleLabel];


}
- (void)layoutSubviews{

    [super layoutSubviews];
    JYWeakSelf;
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.mas_equalTo(weakSelf);
        make.width.height.mas_equalTo(14);
    }];
    [self.selImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.mas_equalTo(weakSelf);
        make.width.height.mas_equalTo(14);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.imgView.mas_right).offset(10);
        make.centerY.mas_equalTo(weakSelf);
    }];
    
    
}
- (UIImageView *)imgView{

    if (!_imgView) {
        _imgView = [[UIImageView alloc]init];
        _imgView.image = [UIImage imageNamed:@"shopping_cart_select"];
    }
    return _imgView;
}
//shopping_cart_select_click
-(UIImageView *)selImgView{

    if (!_selImgView) {
        _selImgView = [[UIImageView alloc]init];
        _selImgView.image = [UIImage imageNamed:@"shopping_cart_select_click"];
        
        
    }
    return _selImgView;

}
- (UILabel *)titleLabel{

    if (!_titleLabel ) {
        _titleLabel =[UILabel labelText:@"" fontSize:13 textColor:k9Color textAlignment:NSTextAlignmentLeft];
    }
    return _titleLabel;

}


@end



@implementation JYRechargeView

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
       
        self.backgroundColor = kBgColor;
       
       
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView{
    
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 44)];
    bgView.backgroundColor = [UIColor whiteColor];
    [bgView borderForColor:[UIColor colorWithHexString:@"DCDDDD"] borderWidth:.1 borderType:UIBorderSideTypeBottom];
    [self addSubview:bgView];
        UITextField * moenyTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, Screen_Width, 35)];
    [moenyTextField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    moenyTextField.placeholder = @"  请输入充值金额";
        [moenyTextField setValue:kFont(13) forKeyPath:@"_placeholderLabel.font"];
    
        [bgView addSubview:moenyTextField];
    _moenyTextField = moenyTextField;
    int totalloc = 3;//一行的列数
    CGFloat appViewW = Screen_Width/3; //按钮的宽度
    CGFloat appViewH = 45; //按钮的高度
    //间隙= （整个屏幕的宽度-总列数*每个的宽度）/（总列数+1）即间隙
    CGFloat margin = 1;
    
    int count = 6; //总按钮的数量
    for(int i = 0 ; i<count;i++){
        int row = i/totalloc;//行号
        int loc = i%totalloc;//列号
        CGFloat appViewX =   (margin + appViewW) * loc; //x值由列号决定
        CGFloat appViewY = CGRectGetMaxY(moenyTextField.frame) + (appViewH+ margin )* row;  //y值由行号决定
        
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(appViewX, appViewY, appViewW, appViewH);
        btn.backgroundColor =  [UIColor whiteColor];
        [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        [btn setTitle:@[@"10",@"50",@"100",@"500",@"1000",@"5000"][i] forState:UIControlStateNormal];
        [btn setTitleColor:k3Color forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(appViewW, appViewH)] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithColor:kThemeColor size:CGSizeMake(appViewW, appViewH)] forState:UIControlStateSelected];
        [self.allArrBtn addObject:btn];
        [self addSubview:btn];
        _btn = btn;
    }
  
}
- (NSMutableArray *)allArrBtn{
    if (!_allArrBtn) {
        _allArrBtn = [NSMutableArray array];
    }
    return _allArrBtn;

}
- (void)textFieldChange:(UITextField *)textField{

    if (_textFieldHandlerOnClick) {
        _textFieldHandlerOnClick(_btn,textField);
    }


}
-(void)btnOnClick:(UIButton *)btn{
    
    _moenyTextField.text = btn.titleLabel.text;
    if (_btnHandlerOnClick) {
        _btnHandlerOnClick(btn,_allArrBtn);
    }



}


@end

